﻿namespace Deppton.Repositories.Filesystem
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using Deppton.Model;
    using Deppton.Model.Xml;
    using System;

    /// <summary>
    /// Filesystem based implementation for surveys repository.
    /// </summary>
    public class FilesystemSurveysRepository : ISurveysRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FilesystemSurveysRepository"/> class.
        /// </summary>
        /// <param name="storagePath">The storage path.</param>
        public FilesystemSurveysRepository(string storagePath)
            : this(storagePath, new FilesystemSurveysAssignationsHandler(storagePath))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FilesystemSurveysRepository"/> class.
        /// </summary>
        /// <param name="storagePath">The storage path.</param>
        /// <param name="surveysAssignationsHandler">The surveys assignations handler.</param>
        public FilesystemSurveysRepository(string storagePath, ISurveysAssignationsHandler surveysAssignationsHandler)
        {
            this.StoragePath = storagePath;
            this.SurveysAssignationHandler = surveysAssignationsHandler;
        }

        /// <summary>
        /// Gets or sets the storage path.
        /// </summary>
        /// <value>The storage path.</value>
        public string StoragePath
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the surveys assignation handler.
        /// </summary>
        /// <value>The surveys assignation handler.</value>
        public ISurveysAssignationsHandler SurveysAssignationHandler
        {
            get;
            set;
        }

        /// <summary>
        /// Finds the pending surveys for the given user.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns>The pending surveys for the given user.</returns>
        public ICollection<Survey> FindPendingSurveysByUser(string userName)
        {
            var surveyIds = this.SurveysAssignationHandler.FindAssignedSurveysForUser(userName);

            var surveys = new List<Survey>();

            foreach (var surveyId in surveyIds)
            {
                var surveyDocument = XDocument.Load(Path.Combine(this.StoragePath, string.Format(CultureInfo.InvariantCulture, "{0}.xml", surveyId)));

                var survey = SurveyXmlHelper.ReadFromElement(surveyDocument.Root);

                if (survey.Status != SurveyStatus.Pending)
                {
                    continue;
                }

                surveys.Add(survey);
            }

            return surveys;
        }

        /// <summary>
        /// Saves the specified survey.
        /// </summary>
        /// <param name="survey">The survey to save.</param>
        public void Save(Survey survey)
        {
            var surveyFilename = Path.Combine(this.StoragePath, String.Format(CultureInfo.InvariantCulture, "{0}.xml", survey.Id));

            SurveyXmlHelper.ConvertToXDocument(survey).Save(surveyFilename);
        }
    }
}
